package com.itbour.web.action.tg;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.tg.TmplSetDto;
import com.itbour.dto.tg.TypeInfoDto;
import com.itbour.service.tg.TmplSetInfoService;
import com.opensymphony.xwork2.ActionContext;


/**
 * 	Design development time 2017 09 25
 * 	@author 李琦
 * 	Special design template group
 */

public class TmplSetInfoAction extends BaseAction {
	private boolean TEST = true;
	private static final long serialVersionUID = 1L;
	
	private TmplSetInfoService tmplSetInfoService;//特殊设计模板
	
	HashMap<String, Object> mapResult = new HashMap<String, Object>();
	
	
	private HashMap<String, Object> tmplSetMap = new HashMap<String, Object>();//[分页] 特殊模板组信息
	private List<TmplSetDto> tmplList;// 模板组信息列表
	private TmplSetDto tmplSetDto;// 更新用
	private String findUse;//用途
	private String findPubFlg;//是否发布
	private String findTmplSTitle;//标题
	private String findTmplTsId;//模板组ID
	private String findDtS;//起始时间
	private String findDtE;//截止时间
	
	//模板组下的设计模板
	private HashMap<String, Object> result = new HashMap<String, Object>();//模板组下的设计模板
	private HashMap<String, Object> sDesignRes = new HashMap<String, Object>();//适合的专题设计模板
	private String findTmplName;// 检索条件(模版名)
	private String findTmplId;// 检索条件(模版id)
	private String findItemId;// 检索条件(关联商品id)
	private String findVip;// 检索条件(vip)
	private String findTmplType;	
	private List<TypeInfoDto> listType;
	
	// [通用参数]
	private String ids;// 通用ids[逗号连接]
	private String order;// 显示顺序
	
	
	
	//分页获取模板组列表
	public String getTmplSetInfoPg() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		Map<String, Object> param = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(findTmplSTitle)) {
			param.put("title", "%"+findTmplSTitle+"%");
		}
		if (StringUtils.isNotEmpty(findTmplTsId)) {
			param.put("ts_id", findTmplTsId);
		}
		if (StringUtils.isNotEmpty(findDtS)) {
			param.put("start_time", findDtS);
		}
		if (StringUtils.isNotEmpty(findDtE)) {
			param.put("end_time", findDtE);
		}
		if (StringUtils.isNotEmpty(findUse)) {
			param.put("use", findUse);
		}
		if (StringUtils.isNotBlank(findPubFlg)) {
			param.put("publish", findPubFlg);
		}
		Page page = tmplSetInfoService.tmplSetInfoPg(param,
				Integer.parseInt(pageNumber),
				Integer.parseInt(pageSize));
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("total", page.getTotalCount());
		resMap.put("rows", page.getResult());
		tmplSetMap = resMap;
		return SUCCESS;
	}
	
	// 加载模板组下的  设计模板目标列表
	public String getTmplGroupDesignInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Integer ts_id = Integer.parseInt(request.getParameter("tsId"));
			if(ts_id!=null){
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("ts_id",ts_id);
				Page page = tmplSetInfoService.getTmplGroupDesignInfo(param,
							Integer.parseInt(pageNumber),
							Integer.parseInt(pageSize));
				HashMap<String, Object> resMap = new HashMap<String, Object>();
				resMap.put("total", page.getTotalCount());
				resMap.put("rows", page.getResult());
				result = resMap;
			}
		}
		return SUCCESS;
	}
	
	/**其他管理/特殊用途模板组  sqlDao/getSuitDesign
	 * 功能：列表查看适合添加到特殊模板组下的设计模板
	 * 参数：tsId模板组ID
	 */
	public String getSuitDesignInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Integer tsId = Integer.parseInt(request.getParameter("tsId"));
			if(tsId!=null){
				Map<String, Object> param = new HashMap<String, Object>();
				if (StringUtils.isNotBlank(findVip)) {
	    			param.put("vip", findVip);
	    		}
				if (StringUtils.isNotBlank(findItemId)) {
	    			param.put("item_id", findItemId);
	    		}
				if (StringUtils.isNotEmpty(findTmplName)) {
	    			param.put("design_name", "%"+findTmplName+"%");
	    		}
				if (StringUtils.isNotBlank(findTmplId)) {
	    			param.put("design_id", findTmplId);
	    		}
				if (StringUtils.isNotBlank(findTmplType)) {
					param.put("type_id", findTmplType);
				}
				param.put("ts_id",tsId);
				Page page = tmplSetInfoService.getSuitDesignInfo(param,
							Integer.parseInt(pageNumber),
							Integer.parseInt(pageSize));
				HashMap<String, Object> resMap = new HashMap<String, Object>();
				resMap.put("total", page.getTotalCount());
				resMap.put("rows", page.getResult());
				sDesignRes = resMap;
			}
		}
		return SUCCESS;
	}
	
	//专题类型下的 适合的设计模板
	public String selectOneType() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer type = Integer.parseInt(request.getParameter("type"));
			if(type!=null){
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("type_id",type);
				listType=tmplSetInfoService.selectOneType(param);
			}
		}
		return SUCCESS;
	}
	
	/**系统目录：其他管理/特殊用途模板组  #itb_tmpl_set_r(模板/模板组关联表)
	 * 功能：给特殊用途/模板组添加适合的设计模板
	 * 参数：ts_id模板组ID/design_id设计模板ID
	 */
	public void addSuitDesign() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer tsId = Integer.parseInt(request.getParameter("tsId"));
			String designId = request.getParameter("design_id");
			if(tsId!=null && designId!=null){
			JSONArray jaArray = JSONArray.fromObject(designId);
			JSONObject jb = null;
			boolean  b = false;//定义判断是否完成
			for (int i = 0; i < jaArray.size(); i++) {
				jb = jaArray.getJSONObject(i);
				System.out.println(jb);
				
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("type",jb.get("top_type"));
				param.put("ts_id",tsId);
				param.put("update_by",user.getUser_id());
				param.put("tmpl_id",jb.get("design_id"));
				b=tmplSetInfoService.addSuitDesign(param);
				}
				if(b){
					this.writeJSON("ok");
				}else{
					this.writeJSON("no");
				}
			}
		}
	}
	
	//移除一个模板组  下的设计模板
	public void delTmplSRDesign() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer stId = Integer.parseInt(request.getParameter("tsId"));
			String designId = request.getParameter("design_id");
			if(designId!=null && stId!=null){
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("tmpl_id",designId);
				param.put("ids",stId);
				param.put("update_by",user.getUser_id());
				boolean b=tmplSetInfoService.delTmplSRDesign(param);
				if(b){
					this.writeJSON("ok");
				}else{
					this.writeJSON("no");
				}
			}
		}
	}
	
	//修改模板组下 设计模板order
	public void updTsrOrder() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer tsId = Integer.parseInt(request.getParameter("tsId"));
			Integer designId = Integer.parseInt(request.getParameter("designId"));
			Integer order = Integer.parseInt(request.getParameter("order"));
			if(designId!=null && tsId!=null){
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("ts_id",tsId);
				param.put("design_id",designId);
				param.put("order",order);
				param.put("update_by",user.getUser_id());
				boolean b=tmplSetInfoService.udpTorder(param);
				if(b){
					this.writeJSON("ok");
				}else{
					this.writeJSON("no");
				}
			}
		}
	}
	
	// 获取分组信息
	public String getTmplSetLst() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("ts_id");
		if (StringUtils.isNotEmpty(id)) {
			param.put("ts_id", id);
		}
		tmplList = tmplSetInfoService.selectTmplSetInfo(param);
		return SUCCESS;
	}
	
	// 删除模板组 
	public void delTmplGp() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if(user!=null && user.getUser_id()>0) {
    		tmplSetInfoService.delTmplGp(ids);
    	}
		this.writeJSON("ok");
	}
	
	// 添加模板组
	public void addTmplSet() throws Exception {
		if (tmplSetDto != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if (user != null) {
	    		tmplSetDto.setUpdate_by(user.getUser_id().toString());
	    	}
	    	if(tmplSetDto.getTs_id()!=null && tmplSetDto.getTs_id()>0){
	    		// 更新
	    		this.updTmplSet();
	    	} else {
	    		// 新建
	    		tmplSetInfoService.addTmplSet(tmplSetDto);
	    		this.writeJSON("ok");
	    	}
		}
	}
	
	// 修改模板组
	public void updTmplSet() throws Exception {
		if (tmplSetDto != null && tmplSetDto.getTs_id()!=null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if(user!=null && user.getUser_id()>0) {
	    		Map<String, Object> param = new HashMap<String, Object>();
		    	param.put("ts_id", tmplSetDto.getTs_id());
		    	param.put("update_by", user.getUser_id());
		    	if(tmplSetDto.getUse() != null && tmplSetDto.getUse()>0) {
		    		param.put("use", tmplSetDto.getUse());
		    	}
		    	if(tmplSetDto.getTitle() != null) {
		    		param.put("title", tmplSetDto.getTitle());
		    	}
		    	if(tmplSetDto.getDesc() != null) {
		    		param.put("desc", tmplSetDto.getDesc());
		    	}
		    	if(tmplSetDto.getThumb() != null) {
		    		param.put("thumb", tmplSetDto.getThumb());
		    	}
		    	if(tmplSetDto.getStart_time() != null) {
		    		param.put("start_time", tmplSetDto.getStart_time());
		    	}
		    	if(tmplSetDto.getEnd_time() != null) {
		    		param.put("end_time", tmplSetDto.getEnd_time());
		    	}
		    	tmplSetInfoService.upTmplSet(param);
	    	}
		}
		this.writeJSON("ok");
	}
	
	// 取消发布专题
	public void cancelTmplSPublish() throws Exception {
		if (StringUtils.isNotBlank(ids)) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if(user!=null && user.getUser_id()!=null){
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("publish", 0);
				param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(ids)));
				param.put("update_by", user.getUser_id());
				tmplSetInfoService.upTmplSet(param);
	    	}
		}
		this.writeJSON("ok");
	}
	
	// 发布模板组
	public void doTmplSPublish() throws Exception {
		if (StringUtils.isNotBlank(ids)) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if(user!=null && user.getUser_id()!=null){
	    		List<Long> listId = StringUtils.stringToList2(ids);

	    		Map<String, Object> param = new HashMap<String, Object>();
				param.put("publish", 1);
				param.put("ids", StringUtils.listToString2(listId));
				param.put("update_by", user.getUser_id());
				tmplSetInfoService.upTmplSet(param);
	    	}
		}
		this.writeJSON("ok");
	}
	
	// 修改模板组 顺序
	public void updTmplSOrder() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if(user!=null && user.getUser_id()!=null){
    		HttpServletRequest request = ServletActionContext.getRequest();
    		String tsId = request.getParameter("tsId");
	    	Map<String, Object> param = new HashMap<String, Object>();
			param.put("order", Integer.valueOf(order));
			param.put("ts_id", Long.valueOf(tsId));
			param.put("update_by", user.getUser_id());
			tmplSetInfoService.upTmplSet(param);
			this.writeJSON("ok");
    	}		
	}

	
	
	
	
	public boolean isTEST() {
		return TEST;
	}
	public void setTEST(boolean tEST) {
		TEST = tEST;
	}
	public TmplSetInfoService getTmplSetInfoService() {
		return tmplSetInfoService;
	}
	public void setTmplSetInfoService(TmplSetInfoService tmplSetInfoService) {
		this.tmplSetInfoService = tmplSetInfoService;
	}
	public HashMap<String, Object> getMapResult() {
		return mapResult;
	}
	public void setMapResult(HashMap<String, Object> mapResult) {
		this.mapResult = mapResult;
	}
	public HashMap<String, Object> getTmplSetMap() {
		return tmplSetMap;
	}
	public void setTmplSetMap(HashMap<String, Object> tmplSetMap) {
		this.tmplSetMap = tmplSetMap;
	}
	public List<TmplSetDto> getTmplList() {
		return tmplList;
	}
	public void setTmplList(List<TmplSetDto> tmplList) {
		this.tmplList = tmplList;
	}
	public TmplSetDto getTmplSetDto() {
		return tmplSetDto;
	}

	public void setTmplSetDto(TmplSetDto tmplSetDto) {
		this.tmplSetDto = tmplSetDto;
	}

	public String getFindUse() {
		return findUse;
	}

	public void setFindUse(String findUse) {
		this.findUse = findUse;
	}

	public String getFindPubFlg() {
		return findPubFlg;
	}

	public void setFindPubFlg(String findPubFlg) {
		this.findPubFlg = findPubFlg;
	}

	public String getFindTmplSTitle() {
		return findTmplSTitle;
	}

	public void setFindTmplSTitle(String findTmplSTitle) {
		this.findTmplSTitle = findTmplSTitle;
	}

	public String getFindTmplTsId() {
		return findTmplTsId;
	}

	public void setFindTmplTsId(String findTmplTsId) {
		this.findTmplTsId = findTmplTsId;
	}

	public String getFindDtS() {
		return findDtS;
	}

	public void setFindDtS(String findDtS) {
		this.findDtS = findDtS;
	}

	public String getFindDtE() {
		return findDtE;
	}

	public void setFindDtE(String findDtE) {
		this.findDtE = findDtE;
	}

	public HashMap<String, Object> getResult() {
		return result;
	}

	public void setResult(HashMap<String, Object> result) {
		this.result = result;
	}

	public HashMap<String, Object> getsDesignRes() {
		return sDesignRes;
	}

	public void setsDesignRes(HashMap<String, Object> sDesignRes) {
		this.sDesignRes = sDesignRes;
	}

	public String getFindTmplName() {
		return findTmplName;
	}

	public void setFindTmplName(String findTmplName) {
		this.findTmplName = findTmplName;
	}

	public String getFindTmplId() {
		return findTmplId;
	}

	public void setFindTmplId(String findTmplId) {
		this.findTmplId = findTmplId;
	}

	public String getFindItemId() {
		return findItemId;
	}

	public void setFindItemId(String findItemId) {
		this.findItemId = findItemId;
	}

	public String getFindVip() {
		return findVip;
	}
	public void setFindVip(String findVip) {
		this.findVip = findVip;
	}

	public String getFindTmplType() {
		return findTmplType;
	}
	public void setFindTmplType(String findTmplType) {
		this.findTmplType = findTmplType;
	}

	public List<TypeInfoDto> getListType() {
		return listType;
	}
	public void setListType(List<TypeInfoDto> listType) {
		this.listType = listType;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
}
